package org.example.codeservce.config;

import org.example.codeservce.service.WebSocketService;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer
{
    @Resource
    private WebSocketService webSocketService;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)
    {
        // 指定WebSocket处理器和路径
        registry.addHandler(new MyWebSocketHandler(webSocketService), "/websocket")
                // 允许所有来源连接，可根据需求设置具体的域名
                .setAllowedOrigins("*");
    }
}